package com.aishua.fire.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.entity.emergencyPre.ExercisePlanDataRespInfo;
import com.aishua.fire.entity.preExercise.DepartmentRespInfo;
import com.aishua.fire.entity.preExercise.ExercisePlanInfo;
import com.aishua.fire.entity.preExercise.ExercisePlanReqInfo;
import com.aishua.fire.entity.preExercise.ExerciseStudyPlanInfo;
import com.aishua.fire.entity.preExercise.ExerciseStudyPlanReqInfo;
import com.aishua.fire.service.PreExerciseManagerService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(value = "预案演练管理", tags = { "预案演练管理相关接口" })
@RestController
public class PreExerciseManagerController {

	@Autowired
	private PreExerciseManagerService preExerciseManagerService;
	
	@Autowired    
	private RuntimeService runtimeService;
	
	@PermMapping(noValidate = true)
	@ApiOperation(value="分页查询_演练计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@GetMapping(value = {"/plan/rehearse"} )
	public ResponseEntity<JsonArrayResult<ExercisePlanInfo>> planList(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "plan_name") String plan_name,@RequestParam(name = "plan_types") String plan_types,@RequestParam(name = "state") String state,
			@RequestParam(name = "time") String time) {
		JsonArrayResult<ExercisePlanInfo> r = null;
		List<ExercisePlanInfo> searchList = preExerciseManagerService.searchPreExerciseList(plan_name,plan_types,time,pageNumber, pageSize,state);
		if(searchList.size()>0) {
			PageInfo<ExercisePlanInfo> pages =new PageInfo<ExercisePlanInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
			return ResponseEntity.ok(r);
		} else {
			r = JsonArrayResult.makeSuccessResult(searchList, 0);
			return ResponseEntity.ok(r);
		}
	}
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="删除_演练计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@DeleteMapping(value = {"/plan/rehearse"} )
	public ResponseEntity<JsonObjectResult>  delPlan(@RequestBody ExercisePlanReqInfo planInfo) {
		 preExerciseManagerService.delPreExercise(planInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="查找_演练责任部门")
	@GetMapping(value = {"/plan/department"} )
	public ResponseEntity<JsonArrayResult>  getPlan() {
		List<DepartmentRespInfo> departInfo =	 preExerciseManagerService.getdepartmentInfo();
		JsonArrayResult r=null;
		if(departInfo!=null) {
			r= JsonArrayResult.makeSuccessResult(departInfo, 0);
		}else {
			r= JsonArrayResult.makeErrorResult(departInfo);
		}
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="添加_演练计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@PostMapping(value = {"/plan/rehearse"} )
	public ResponseEntity<JsonObjectResult>  addPlan( @RequestBody ExercisePlanReqInfo planInfo) {
	    //初始状态为2未执行
		planInfo.setState("2");
		 preExerciseManagerService.addPreExercise(planInfo);
		 log.info("任务id:{}",planInfo.getId());
		 Map<String,Object> variables = new HashMap<String,Object>();
		 variables.put("plan_name", planInfo.getPlan_name());
		variables.put("plan_types",planInfo.getPlan_types());
		variables.put("rehearse_time",planInfo.getRehearse_time());
		variables.put("stste",planInfo.getState());
		ProcessInstance pi = runtimeService.startProcessInstanceByKey("drill",planInfo.getId(),variables);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="修改_演练计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@PutMapping(value = {"/plan/rehearse"} )
	public ResponseEntity<JsonObjectResult>  updatePlan( @RequestBody ExercisePlanReqInfo planInfo) {
		 preExerciseManagerService.updatePreExercise(planInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="添加_预案学习计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exerciseStudyPlan)
	@PostMapping(value = {"/plan/study"} )
	public ResponseEntity<JsonObjectResult>  addStudyPlan( @RequestBody ExerciseStudyPlanReqInfo studyInfo) {
		 preExerciseManagerService.addStudyPlan(studyInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@PermMapping(noValidate = true)
	@ApiOperation(value="分页查询_预案学习计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exerciseStudyPlan)
	@GetMapping(value = {"/plan/study"} )
	public ResponseEntity<JsonArrayResult<ExerciseStudyPlanInfo>> getStudyPlan(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "study_name") String plan_name,@RequestParam(name = "plan_types") String plan_types,
			@RequestParam(name = "study_time") String study_time,@RequestParam(name = "prepare_name")String prepare_name) {
		List<ExerciseStudyPlanInfo> searchList = preExerciseManagerService.searchStudyPlanList(plan_name,plan_types,study_time,prepare_name,pageNumber, pageSize);
		int total =preExerciseManagerService.searchStudyPlanTotal(plan_name,plan_types,study_time,prepare_name);
		log.info("分页学习计划条数：{}",total);
		JsonArrayResult<ExerciseStudyPlanInfo> r = JsonArrayResult.makeSuccessResult(searchList,total);
		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="删除_预案学习计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exerciseStudyPlan)
	@DeleteMapping(value = {"/plan/study"} )
	public ResponseEntity<JsonObjectResult>  delStudyPlan(@RequestBody ExerciseStudyPlanReqInfo studyInfo) {
		 preExerciseManagerService.delStudyPlan(studyInfo.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@PermMapping(noValidate = true)
	@ApiOperation(value="修改_预案学习计划接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exerciseStudyPlan)
	@PutMapping(value = {"/plan/study"} )
	public ResponseEntity<JsonObjectResult>  updateStudyPlan( @RequestBody ExerciseStudyPlanReqInfo planInfo) {
		 preExerciseManagerService.updateStudyPlan(planInfo);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
	}
	
	@PermMapping(noValidate = true)
	@ApiOperation(value="分页查询_演练资料接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@GetMapping(value = {"/plan/preData"} )
	public ResponseEntity<JsonArrayResult<ExercisePlanDataRespInfo>> dataList(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize,
			@RequestParam(name = "plan_name") String plan_name,@RequestParam(name = "plan_types") String plan_types,@RequestParam(name = "state") String state,
			@RequestParam(name = "time") String time) {
		JsonArrayResult<ExercisePlanDataRespInfo> r = null;
		List<ExercisePlanDataRespInfo> searchList = preExerciseManagerService.searchPreExerciseDataList(plan_name,plan_types,time,pageNumber, pageSize,state);
		if(searchList.size()>0) {
			PageInfo<ExercisePlanDataRespInfo> pages =new PageInfo<ExercisePlanDataRespInfo>(searchList);
			r = JsonArrayResult.makeSuccessResult(pages.getList(), (int) pages.getTotal());
			return ResponseEntity.ok(r);
		} else {
			r = JsonArrayResult.makeSuccessResult(searchList, 0);
			return ResponseEntity.ok(r);
		}
	}
	
	@PermMapping(noValidate = true)
	@ApiOperation(value="查询_演练资料详情接口")
	@ModuleService(totalModule=BigMenuConstants.preExercise,moduleName=ModuleConstants.exercisePlan)
	@GetMapping(value = {"/plan/DataDetails"} )
	public ResponseEntity<JsonArrayResult<ExercisePlanDataRespInfo>> dataDetails(
			@RequestParam(name = "id") String id) {
		ExercisePlanDataRespInfo searchList = preExerciseManagerService.searchdataDetails(id);
		List<ExercisePlanDataRespInfo> list =new ArrayList<ExercisePlanDataRespInfo>();
		list.add(searchList);
		JsonArrayResult<ExercisePlanDataRespInfo> r = JsonArrayResult.makeSuccessResult(list, 1);
		return ResponseEntity.ok(r);
		
	}
	
	
}
